package com.wl.service.impl;

import com.wl.domain.AjaxResult;
import com.wl.domain.Product;
import com.wl.domain.Store;
import com.wl.dto.ProductDto;
import com.wl.feign.FeignServer;
import com.wl.mapper.ProductMapper;
import com.wl.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private FeignServer feignServer;
    @Override
    @Transactional
    public AjaxResult steProduct(ProductDto productDto) {
        //创建product对象
        Product product = new Product();
        //设置参数
        product.setName(productDto.getName());
        product.setPrice(productDto.getPrice());
        //添加到数据库
        productMapper.save(product);
        //创建store对象
        Store store = new Store();
        //获取自增id
        store.setProduct_id(product.getId());
        //设置仓库数量
        store.setNumberA(productDto.getNumberA());
        //调用store的新增方法
        AjaxResult ajaxResult = feignServer.save(store);
        //如果返回状态为false就删掉
        if (ajaxResult.getSuccess() == false){
            productMapper.del(product.getId());
        }
        //返回结果集
        return ajaxResult;
    }
}
